package cn.interestingshop;

import cn.interestingshop.entity.User;
import cn.interestingshop.utils.JedisPoolUtils;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : JedisTest
 * @Description :
 * @Author : Lzz
 * @Date: 2023/3/3 15:49:10
 */
public class JedisTest {

    public static void main(String[] args) {
        //创建Jedis对象
        //redis存取javaBean
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setId(1);
        user.setNickName("张三");
        user.setPhone("13200000000");
        userList.add(user);
        User user1 = new User();
        user1.setId(2);
        user1.setNickName("王麻子");
        user1.setPhone("131111111111");
        userList.add(user1);
        //将java泛型集合存入Redis
        Jedis jedis = JedisPoolUtils.getJedis();
        jedis.set("users", JSON.toJSONString(userList));
        System.out.println("将users对象存入redis成功！");
        String users = jedis.get("users");
        List<User> userList1 = JSON.parseArray(users, User.class);
        for (User user2 : userList1) {
            System.out.println(user2.getNickName());
        }

        /*Jedis jedis = JedisPoolUtils.getJedis();
        jedis.set("userName", "李四");
        jedis.expire("userName", 120);
        String name = jedis.get("userName");
        System.out.println("userName："+name);*/
        /*Jedis jedis = new Jedis("127.0.0.1",6379);
        //set(key,value)
        jedis.set("username", "张三");
        //get(key)
        String name = jedis.get("username");
        System.out.println("存入redis成功！");
        System.out.println("username："+name);*/



    }

}
